// miniprogram/pages/order_list/order_list.js

import req from "../../utils/request";
import { POST_ORDER_MYORDERLIST,POST_ORDER_HAIILISTPOST,USER_ORDER_LIST } from "../../messages/uris";

const app = getApp();
Page({
  data: {
    tabTitle: null, // 当前所在页面的title
    clientHeight: "",
    pageNo:1,//当前页
    userOrderPage: 0,//总条数
    tabs: null,
    orderList: [{}],
    deliver_list: [{}], 
    //弹出层
    topTips: false,
    hide: false,
    tipValue: "",
    currentStatus:null, //当前页面查询的状态
    role: 0  //权限标识
  },

  /** */
  onTabsChange(e) {
    const { key } = e.detail;
    const index = this.data.tabs.map((n) => n.key).indexOf(key);
    this.setData({
      key,
      index,
    });
    /**每次切换tabs 需要把当前页 和 全部页数  赋值为原始值 */
    this.setData({
      userOrderPage:0,
      pageNo:1,
      orderList: [{}]
    })
    this.getDataKey(e);
  },

  onSwiperChange(e) {
    const { current: index, source } = e.detail;
    const { key } = this.data.tabs[index];

    if (!!source) {
      this.setData({
        key,
        index,
      });
    }
  },

  /**
   * 取消订单
   */
  cancel: function (event) {
    const that = this;
    var id = event.target.id;
    wx.showModal({
      title: "确认",
      content: "确定要取消订单吗？",
      confirmColor: "#ed3f14",
      success: (res) => {
        /* TODO 取消订单 */
      },
    });
  },

  /**
   * 确认收货方法
   */
  confirm: function (event) {
    var id = event.target.id;
    const that = this;
    wx.showModal({
      title: "确认",
      content: "确认收到货了吗？",
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({
            title: "请稍后",
            mask: "true",
          });
          // TODO 确认收获
        } else if (!res.confirm) {
          return;
        }
      },
    });
  },


  call: function (event) {
    var phone = event.target.id;
    wx.makePhoneCall({
      phoneNumber: phone,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 要求登陆
    if (app.globalData.login == false) {
      wx.showToast({
        title: "未注册",
      });
      // return;
      wx.redirectTo({
        url: "../index/index",
      });
    }
    // 同步
    wx.showLoading({
      title: "加载中",
    });
    setTimeout(function () {
      wx.hideLoading()
    }, 2000)
    const that = this;
    //获取设备屏幕大小
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          clientHeight: res.windowHeight,
        });
      },
    });
    var d = new Date();
  },

  onTabItemTap: function (item) {
    console.log(item)
  },

  getDataKey:function(e){
    var key = e.detail.key;
    this.getOrderStatus(key)
  },

  getOrderStatus:function(key){
    var that = this;
    var status = null;
    var url = null;
    //获取真正查询的url地址
    if(key == 'tab1'){     //待付款
      status = 101;
      url = POST_ORDER_MYORDERLIST;
    }else if(key == 'tab2'){   //接单大厅(已经付款)
      status = 102;
      url = POST_ORDER_HAIILISTPOST;
    } else if(key == 'tab3'){  //未取件
      status = 103;
      url = POST_ORDER_MYORDERLIST;
    }else if(key == 'tab4'){  //已取件
      status = 104;
      url = POST_ORDER_MYORDERLIST;
    }else if(key == 'tab5'){  //已完成
      status = 105;
      url = POST_ORDER_MYORDERLIST;
    }
    //设置所再页面的定位
    that.setData({
      tabTitle:key,
      currentStatus:status
    });
    console.log("当前所在窗口的:" + key);
    that.selectUserOrder(status,url)
  },
  
  /**
   * 公共查询方法
   */
  selectUserOrder:function(status,url){
    const that = this;
    //拿总页数 和 当前页进行比较 当前页不能大于总页数
    if(that.data.userOrderPage >= that.data.pageNo || that.data.pageNo == 1){
      req.get(
        url,
        { pageNo: that.data.pageNo, pageSize: 10,orderStatus:status},
        (res) => {
          console.log("查询了一次");
          var list = that.data.orderList;//获取页面数据集合
          if(res.data.result.records.length > 0){
            for(var i = 0; i < res.data.result.records.length; i++){
              list.push(res.data.result.records[i]);
            }
            var pageNo = parseInt(res.data.result.current) + 1;  //获取当前页 + 1 查询下一页数据
            var page = res.data.result.pages;//总页数
            //重新赋值 当前页和 总页数
            that.setData({orderList:list,pageNo: pageNo,userOrderPage:page});
          }else{
            console.log("没有更多数据了");
          }
        },
        (res) => {},
        (res) => {
          wx.hideLoading();
        },that
      );
    }
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    let that = this;
    if(that.data.role == 1){
      that.setData({
        tabs:[
          {
            key: "tab1",
            title: "待付款",
          },
          {
            key: "tab3",
            title: "待取件",
          },
          {
            key: "tab4",
            title: "待收货",
          },
          {
            key: "tab5",
            title: "已完成",
          },
        ],
        tabTitle: "tab1",
      });
    }else{
      that.setData({
        tabs:[
          {
            key: "tab2",
            title: "接单大厅",
          },
          {
            key: "tab3",
            title: "待取件",
          },
          {
            key: "tab4",
            title: "已取件",
          },
          {
            key: "tab5",
            title: "已完成",
          },
        ],
        tabTitle: "tab2",
      });
    }
    that.getOrderStatus(that.data.tabTitle);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},

  /**
  *滚动到底部/右边，会触发 scrolltolower 事件
  */
 bindDownLoad: function () {
    console.log("滚动到底部/右边")
    let that = this;
    //重新获取分页值+1
    that.getOrderStatus(this.data.tabTitle);
  },

  /**
  * 滚动到顶部/左边，会触发 scrolltoupper 事件
  */
//  topLoad:function(){
//     console.log("滚动到顶部/左边")
//   }
  

});
